package com.lening.provider.controller.OwnersDecoration;

import com.lening.common.base.BaseController;
import com.lening.provider.dto.OwnersDecoration.RelatedEvidenceDTO;
import com.lening.provider.po.OwnersDecoration.RelatedEvidencePO;
import com.lening.provider.service.OwnersDecoration.RelatedEvidenceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 刘政华
 * @datetime 2024/6/5 11:53
 * @description 类对象
 */
@Slf4j
@RestController
@RequestMapping("relatedEvidences")
public class RelatedEvidenceController extends BaseController<RelatedEvidencePO, RelatedEvidenceDTO, RelatedEvidenceService> {

    @Value("${upload.imagePath}")
    private String uploadPath;

    @Resource
    private RelatedEvidenceService relatedEvidenceService;


    @PostMapping("/upload")
    public String uploadImages(@RequestParam("files") MultipartFile[] files) {
        List<String> fileNames = new ArrayList<>();
        try {
            for (MultipartFile file : files) {
                // 获取文件名
                String fileName = file.getOriginalFilename();
                if (fileName != null && !fileName.isEmpty()) {
                    // 指定保存路径
                    String evidence = uploadPath + fileName;
                    // 保存文件到本地
                    file.transferTo(new File(evidence));
                    fileNames.add(evidence); // 可以收集文件名用于后续处理或返回

                    // 创建DecorationDrawingsPO对象并存储到数据库（这里假设每个文件都创建一个记录）
                    RelatedEvidencePO relatedEvidencePO = new RelatedEvidencePO();
                    relatedEvidencePO.setEvidence(evidence);
                    relatedEvidenceService.add(relatedEvidencePO);
                }
            }
            return "批量上传成功";
        } catch (IOException e) {
            e.printStackTrace();
            return "批量上传失败";
        }
    }
//    @PostMapping("/upload")
//    public String uploadImage(@RequestParam("file") MultipartFile file) {
//        try {
//            // 获取文件名
//            String fileName = file.getOriginalFilename();
//            // 指定保存路径
//            String evidence = uploadPath + fileName;
//            // 保存文件到本地
//            file.transferTo(new File(evidence));
//
//            //创建RelatedEvidencePO对象并存储到数据库
//            RelatedEvidencePO relatedEvidencePO = new RelatedEvidencePO();
//            relatedEvidencePO.setEvidence(evidence);
//            relatedEvidenceService.add(relatedEvidencePO);
//
//            return "上传成功";
//        } catch (IOException e) {
//            e.printStackTrace();
//            return "上传失败";
//        }
//    }

}
